<?php
include_once("../service/CategoryService.php");
/**
 * @author Minh Luan
 * @copyright 2012
 */
if(isset($_POST['cat_name_en']) && isset($_POST['cat_name_vn'])){
    $cat_name_en = $_POST['cat_name_en'];
    $cat_name_vn = $_POST['cat_name_vn'];
    $description_en = $_POST['cat_description_en'];
    $description_vn = $_POST['cat_description_vn'];
    $cat_parent_id = $_POST['cat_parent_id'];
    
    if(isCategoryExist($cat_name_en, $cat_name_vn)){
        echo "<script>location.href='../admin/category-detail.php?do=add&msg=5'</script>";
    }
    
    addCategory($cat_name_en,$cat_name_vn,$description_en,$description_vn,$cat_parent_id);
    
    $newestId = mysql_insert_id();
    
    if($newestId > 0){
        if($_FILES['cat_image']['name'] != ''){
            if ((($_FILES["cat_image"]["type"] == "image/gif") || ($_FILES["cat_image"]["type"] == "image/jpeg") || ($_FILES["cat_image"]["type"] == "image/jpg")) && ($_FILES["cat_image"]["size"] < 1048576))
            {
                if ($_FILES["cat_image"]["error"] > 0)
                {
                echo "Return Code: " . $_FILES["cat_image"]["error"] . "<br />";
                }
                else
                {
                  move_uploaded_file($_FILES["cat_image"]["tmp_name"],"../images/cat/CAT_".$newestId.".jpg");
                }
                updateCategoryImageUrl($newestId, "CAT_".$newestId.".jpg");
                echo "<script>location.href='../admin/category.php?msg=1'</script>";
            }
            else
            {
                echo "Invalid file";
                echo "Upload: " . $_FILES["cat_image"]["name"] . "<br />";
                echo "Type: " . $_FILES["cat_image"]["type"] . "<br />";
                echo "Size: " . ($_FILES["cat_image"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["cat_image"]["tmp_name"] . "<br />";
                echo "<script>location.href='../admin/category-detail.php?do=add&msg=3'</script>"; 
            }
        }else{
            echo "<script>location.href='../admin/category.php?msg=2'</script>";
        }
    }else{
        echo "<script>location.href='../admin/category-detail.php?do=add&msg=4'</script>";        
    }
}
else
{
    echo "<script>location.href='../admin/category-detail.php?do=add'</script>";
}


?>